﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;

namespace QLKH.PopUp.Thicong
{
    public partial class popThicong : Form
    {
        private Int64 idcus = -1;
        private Int16 idduong = -1;
        public popThicong(Int64 id, Int16 idd)
        {
            InitializeComponent();
            idcus = id;
            idduong = idd;
        }

        private void binCombo(ComboBox cb, DataTable dt, string name, string value, string option)
        {
            List<trangthai> list = new List<trangthai>();
            trangthai ls = null;
            ls = new trangthai();
            ls.name = option;
            ls.value = "0";
            list.Add(ls);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ls = new trangthai();
                    ls.name = dt.Rows[i][name].ToString();
                    ls.value = dt.Rows[i][value].ToString();
                    list.Add(ls);
                }
            }
            cb.DataSource = list;
            cb.DisplayMember = "name";
            cb.ValueMember = "value";
        }

        private void popThicong_Load(object sender, EventArgs e)
        {
            //loai dong ho
            clDH dh = new clDH();
            DataTable dtdh = dh.getdata();
            binCombo(cbldh, dtdh, "ten", "id", "<Chọn loại đồng hồ>");
            //to lap
            DataTable dttl = dh.gettolap();
            binCombo(cbtolap, dttl, "ten", "id", "<Chọn tổ lắp>");
            //dong ho tong
            clDHTong dht = new clDHTong();
            DataTable dtdht = dht.getdata(1);
            binCombo(cbdht, dtdht, "tenDHTong", "id", "<Chọn đồng hồ tổng>");

            //tuyen
            clTuyen tuyen = new clTuyen();
            DataTable dttuyen = tuyen.danhsachtuyen();
            binCombo(cbbTuyen, dttuyen, "tenTuyen", "id", "<Chọn tuyến>");
            //Autocomplete
            clKhachhang kh = new clKhachhang();
            AutoCompleteStringCollection dskhachhang = new AutoCompleteStringCollection();
            DataTable dtds = kh.getautocomplete(idduong, 0);
            if (dtds != null && dtds.Rows.Count > 0)
            {
                foreach (DataRow row in dtds.Rows) {
                    dskhachhang.Add(row["soHD"].ToString());
                }
            }
            //txtkhtong.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //txtkhtong.AutoCompleteSource = AutoCompleteSource.CustomSource;
            //txtkhtong.AutoCompleteCustomSource = dskhachhang;

            //txtkhvitri.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //txtkhvitri.AutoCompleteSource = AutoCompleteSource.CustomSource;
            //txtkhvitri.AutoCompleteCustomSource = dskhachhang;
        }

        private void cbdht_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                Int16 iddhtong = Int16.Parse((cbdht.SelectedValue == null) ? "0" : cbdht.SelectedValue.ToString().Trim());
                clKhachhang kh = new clKhachhang();
                AutoCompleteStringCollection dskhachhang = new AutoCompleteStringCollection();
                DataTable dtds = kh.getautocomplete(idduong, iddhtong);
                if (dtds != null && dtds.Rows.Count > 0)
                {
                    foreach (DataRow row in dtds.Rows)
                    {
                        dskhachhang.Add(row["soHD"].ToString() + row["tenKH"].ToString());
                    }
                }
                //txtkhtong.Text = string.Empty;
                //txtkhtong.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                //txtkhtong.AutoCompleteSource = AutoCompleteSource.CustomSource;
                //txtkhtong.AutoCompleteCustomSource = dskhachhang;

                //txtkhtong.Text = string.Empty;
                //txtkhvitri.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                //txtkhvitri.AutoCompleteSource = AutoCompleteSource.CustomSource;
                //txtkhvitri.AutoCompleteCustomSource = dskhachhang;
            }
            catch { }
        }
        // khai báo 1 hàm delegate
        public delegate void ThiCong(int outid);
        // khai báo 1 kiểu hàm delegate
        public ThiCong MyGetData;

        private void btsave_Click(object sender, EventArgs e)
        {
            if (MyGetData != null)
            {// tại đây gọi nó
                Int64 idkh = idcus;
                string madh = txtmadh.Text.Trim();
                Int16 idldh = Int16.Parse((cbldh.SelectedValue == null) ? "0" : cbldh.SelectedValue.ToString().Trim());
                string kichco = txtkichco.Text.Trim();
                Int16 idtolap = Int16.Parse((cbtolap.SelectedValue == null) ? "0" : cbtolap.SelectedValue.ToString().Trim());
                string tolap = (cbtolap.SelectedValue == null || cbtolap.SelectedValue.ToString() == "0") ? "" : cbtolap.Text.Trim();
                Int64 ngaylap = Int64.Parse(dateday.Text.Trim().Split('/')[2].ToString() + dateday.Text.Trim().Split('/')[1].ToString() + dateday.Text.Trim().Split('/')[0].ToString());
                Int16 iddhtong = Int16.Parse((cbdht.SelectedValue == null) ? "0" : cbdht.SelectedValue.ToString().Trim());
                string tendhtong = (cbdht.SelectedValue == null || cbdht.SelectedValue.ToString() == "0") ? "" : cbdht.Text.Trim();
                Int16 idTuyen = Int16.Parse((cbbTuyen.SelectedValue == null) ? "0" : cbbTuyen.SelectedValue.ToString().Trim());
                string tenTuyen = cbbTuyen.Text.Trim();
                //string khtong = txtkhtong.Text.Trim();
                string dutoan = txtchitiet.Text.Trim();

                Nullable<float> tongdt;

                if (txtdutoan.Text == "")
                    tongdt = null;
                else
                    tongdt = float.Parse(txtdutoan.Text.Trim());
                //string mavitri = txtkhvitri.Text.Trim();
                //Int16 type = Int16.Parse((rdotruoc.Checked == true) ? "1" : "0");
                Nullable<int> chisoDH;

                if (txtchisoDH.Text == "")
                    chisoDH = null;
                else
                    chisoDH = int.Parse(txtchisoDH.Text.Trim());

                string motalapdat = txtmotaLapDat.Text.Trim();
                Int16 trangthai = Int16.Parse("3");
                clKhachhang clkh = new clKhachhang();
                int outid = clkh.thicong(idkh, madh, idldh, kichco,idtolap, tolap, ngaylap, iddhtong, tendhtong, dutoan, tongdt, chisoDH, trangthai,motalapdat,idTuyen,tenTuyen);
                MyGetData(outid);
                //update khach hang moi vào tbsudung
                Int16 index_tbsudung = -1;
                string str_tbsudung = "tbSudung" + ngaylap.ToString().Substring(0, 6);
                clTaoTbSuDung cltsd = new clTaoTbSuDung();
                DataTable tbtsd = cltsd.listTableSudung();
                if (tbtsd.Rows.Count > 0)
                {
                    for (Int16 i = 0; i < tbtsd.Rows.Count; i++)
                    {
                        if (tbtsd.Rows[i][0].ToString().Trim() == str_tbsudung)
                        {
                            index_tbsudung = i;
                            break;
                        }
                    }
                    if (index_tbsudung != -1)
                    {
                        cltsd.update_khmoi_tbsudung(idkh, str_tbsudung);
                    }
                }
                //end update khach hang moi vao tbsudung
            }
            this.Close();
        }

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
